<!--
 * @Description: 项目根组件
 * @Author: hai-27
 * @Date: 2020-02-07 16:23:00
 * @LastEditors: chencaoyang
 * @LastEditTime: 2021-06-07 11:29:46
 -->
<template>
  <div id="app" name="app">
    <el-container>
      <!-- 顶部导航栏 -->
      <div class="topbar">
        <div class="nav">
          <ul>
            <li v-if="!token">
              <el-button type="text" class="loginText" @click="login"
                >您好，请登录</el-button
              >
              <el-button type="text" @click="registerFun">免费注册</el-button>
              <span class="sep">|</span>
            </li>
            <li v-else style="cursor: pointer">
              欢迎
              <el-popover placement="top" width="180" v-model="visible">
                <p>确定退出登录吗？</p>
                <div style="text-align: right; margin: 10px 0 0">
                  <el-button size="mini" type="text" @click="visible = false"
                    >取消</el-button
                  >
                  <el-button type="primary" size="mini" @click="logout"
                    >确定</el-button
                  >
                </div>
                <el-button type="text" slot="reference">
                  {{ userInfo.userName }}
                </el-button>
              </el-popover>
            </li>
            <li>
              <router-link to="/order?isShoppingCart=1">
                <i class="el-icon-shopping-cart-full"></i> 购物车
                <span class="sep">|</span>
                <!-- <span class="num">({{getNum}})</span> -->
              </router-link>
            </li>
            <li>
              <router-link to="/order">个人中心</router-link>
            </li>
            <!-- <li>
              <router-link to="/order">我的订单</router-link>
            </li>
            <li>
              <router-link to="/collect">我的收藏</router-link>
            </li>-->
          </ul>
        </div>
      </div>
      <!-- 顶部导航栏END -->

      <!-- 顶栏容器 -->
      <el-header>
        <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          active-text-color="#CF975A"
          router
        >
          <el-menu-item index="/" :style="{ paddingLeft: 0 }"
            >首页</el-menu-item
          >
          <el-menu-item index="/goods">旅游特产</el-menu-item>
          <el-menu-item index="/panda">大熊猫</el-menu-item>
          <el-menu-item index="/tourism">旅游资源</el-menu-item>
          <el-menu-item index="/community">社区服务</el-menu-item>
          <el-menu-item index="/counsel">旅游资讯</el-menu-item>
        </el-menu>
        <div class="so">
          <el-input placeholder="请输入搜索内容" v-model="search">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchClick"
            ></el-button>
          </el-input>
        </div>
      </el-header>
      <!-- 顶栏容器END -->

      <!-- 登录模块 -->
      <MyLogin></MyLogin>
      <!-- 注册模块 -->
      <MyRegister :register="register" @fromChild="isRegister"></MyRegister>

      <!-- 主要区域容器 -->
      <el-main>
        <router-view></router-view>
      </el-main>
      <!-- 主要区域容器END -->

      <!-- 底栏容器 -->
      <!-- 底部组件 -->
      <bottom />
      <!-- 底栏容器END -->
    </el-container>
  </div>
</template>

<script>
import bottom from "./components/bottom";
import { get_data, clear_data } from "./utils/utils";
export default {
  components: {
    bottom,
  },
  beforeUpdate() {
    this.activeIndex = this.$route.path;
  },
  data() {
    return {
      activeIndex: "", // 头部导航栏选中的标签
      search: "", // 搜索条件
      register: false, // 是否显示注册组件
      visible: false, // 是否退出登录
      token: "",
      userInfo: {},
    };
  },
  created() {
    this.token = get_data("token");
    if (this.token) {
      this.userInfo = JSON.parse(get_data("userInfo"));
    }
    // 获取浏览器localStorage，判断用户是否已经登录
    /* window.setTimeout(() => {
      this.$message({
        duration: 0,
        showClose: true,
        message: `
        <p>如果觉得这个项目还不错，</p>
        <p style="padding:10px 0">您可以给项目源代码仓库点Star支持一下，谢谢！</p>
        <p><a href="https://github.com/hai-27/vue-store" target="_blank">Github传送门</a></p>`,
        dangerouslyUseHTMLString: true,
        type: "success"
      });
    }, 1000 * 60); */
  },
  computed: {},
  watch: {
    $route: {
      handler: function () {
        this.token = get_data("token");
        if (this.token) {
          this.userInfo = JSON.parse(get_data("userInfo"));
        }
        document.body.scrollTop = 0;
        document.documentElement.scrollTop = 0;
      },
    },
  },
  methods: {
    login() {
      this.$router.push("/login");
    },
    registerFun() {
      this.$router.push("/registered");
    },
    // 退出登录
    logout() {
      clear_data("token");
      clear_data("userInfo");
      this.$router.push({ path: "/" });
    },
    // 接收注册子组件传过来的数据
    isRegister(val) {
      console.log(val + "接受组件传过来的参数");
      this.register = val;
    },
    // 点击搜索按钮
    searchClick() {
      if (this.search != "") {
        // 跳转到全部商品页面,并传递搜索条件
        console.log(this.activeIndex.split("/"));
        let activeIndex = "/" + this.activeIndex.split("/")[1];

        this.$router.push({
          path: activeIndex,
          query: { search: this.search },
        });
        this.search = "";
      }
    },
  },
};
</script>

<style lang="less">
/* 全局CSS */
* {
  padding: 0;
  margin: 0;
  border: 0;
  list-style: none;
}
img {
  object-fit: fill;
}
#app .el-header {
  padding: 0;
  height: 75px;
}
#app .el-main {
  min-height: 300px;
  padding: 0;
}
#app .el-footer {
  padding: 0;
}
a,
a:hover {
  text-decoration: none;
}
/* 全局CSS END */

/* 顶部导航栏CSS */
.topbar {
  height: 40px;
  background: #f8f8f8;
}
.topbar .nav {
  width: 1225px;
  margin: 0 auto;
}
.topbar .nav ul {
  float: right;
}
.topbar .nav li {
  float: left;
  height: 40px;
  color: #b0b0b0;
  font-size: 14px;
  text-align: center;
  line-height: 40px;
  // margin-left: 20px;
}
.topbar .nav .sep {
  color: #b0b0b0;
  font-size: 14px;
  margin: 0 10px;
}
.topbar .nav li .el-button {
  color: #b0b0b0;
}
// .topbar .nav .el-button:hover {
//   color: #fff;
// }
.topbar .nav li a {
  color: #b0b0b0;
}
// .topbar .nav a:hover {
//   color: #fff;
// }
.topbar .nav .shopCart {
  // width: 120px;
  // background: #424242;
  margin-left: 0;
}
// .topbar .nav .shopCart:hover {
//   background: #fff;
// }
// .topbar .nav .shopCart:hover a {
//   color: #ff6700;
// }
.topbar .nav .shopCart-full {
  // width: 120px;
  // background: #ff6700;
}
// .topbar .nav .shopCart-full a {
//   color: white;
// }
.loginText {
  color: #cf975a !important;
  font-size: 14px;
  margin-right: 20px;
}
.el-button + .el-button {
  margin-left: 0;
}
/* 顶部导航栏CSS END */

/* 顶栏容器CSS */
.el-header {
  height: 75px !important;
  width: 1225px;
  margin: 0 auto;
  overflow: hidden;
  // border: 1px solid;
  position: relative;
}
.el-header .el-menu {
  border-bottom: none !important;
}
.el-header .el-menu li {
  height: 59px;
  line-height: 76px;
}

.el-header .so {
  position: absolute;
  width: 300px;
  right: 0;
  top: 50%;
  margin-top: -18px;
}
/* 顶栏容器CSS END */

/* 底栏容器CSS */
.footer {
  width: 100%;
  text-align: center;
  background: #2f2f2f;
  padding-bottom: 20px;
}
.footer .ng-promise-box {
  border-bottom: 1px solid #3d3d3d;
  line-height: 145px;
}
.footer .ng-promise-box {
  margin: 0 auto;
  border-bottom: 1px solid #3d3d3d;
  line-height: 145px;
}
.footer .ng-promise-box .ng-promise p a {
  color: #fff;
  font-size: 20px;
  margin-right: 210px;
  padding-left: 44px;
  height: 40px;
  display: inline-block;
  line-height: 40px;
  text-decoration: none;
  background: url("./assets/imgs/us-icon.png") no-repeat left 0;
}
.footer .github {
  height: 50px;
  line-height: 50px;
  margin-top: 20px;
}
.footer .github .github-but {
  width: 50px;
  height: 50px;
  margin: 0 auto;
  background: url("./assets/imgs/github.png") no-repeat;
}
.footer .mod_help {
  text-align: center;
  color: #888888;
}
.footer .mod_help p {
  margin: 20px 0 16px 0;
}

.footer .mod_help p a {
  color: #888888;
  text-decoration: none;
}
.footer .mod_help p a:hover {
  color: #fff;
}
.footer .mod_help p span {
  padding: 0 22px;
}
/* 底栏容器CSS END */
</style>
